package jobs;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import models.Customer;
import models.DeliverySpot;
import models.DeliverySpotValues;
import models.DeliverySpotValuesId;
import models.Establishment;
import models.MenuItem;
import models.Order;
import models.Order.OrderJsonSerializer;
import models.OrderItem;
import models.SpotConfigurationAttribute;
import play.jobs.Every;
import play.jobs.Job;
import event.EventHandler;

public class TestWebSocketJob extends Job {
	
	@Override
	public void doJob() throws Exception {
		saveOrder();
	}
	
	private void saveOrder() {
		Establishment establishment = Establishment.findById(5L);
		Customer c = Customer.findById(13L);
		List<MenuItem> items = MenuItem.findAll();

		DeliverySpot spot = new DeliverySpot();
		spot.save();
		
		DeliverySpotValuesId id1 =  new DeliverySpotValuesId();
		id1.setDeliverySpot(spot);
		SpotConfigurationAttribute attribute1 = SpotConfigurationAttribute.findById(15L);
		id1.setAttribute(attribute1);
		DeliverySpotValues value1 = new DeliverySpotValues();
		value1.setDeliverySpotValuesId(id1);
		value1.setValue("AA");
		value1.save();

		DeliverySpotValuesId id2 =  new DeliverySpotValuesId();
		id2.setDeliverySpot(spot);
		SpotConfigurationAttribute attribute2 = SpotConfigurationAttribute.findById(16L);
		id2.setAttribute(attribute2);
		DeliverySpotValues value2 = new DeliverySpotValues();
		value2.setDeliverySpotValuesId(id2);
		value2.setValue("Z");
		value2.save();

		DeliverySpotValuesId id3 =  new DeliverySpotValuesId();
		id3.setDeliverySpot(spot);
		SpotConfigurationAttribute attribute3 = SpotConfigurationAttribute.findById(17L);
		id3.setAttribute(attribute3);
		DeliverySpotValues value3 = new DeliverySpotValues();
		value3.setDeliverySpotValuesId(id3);
		value3.setValue("23");
		value3.save();
		
		Set<DeliverySpotValues> values = new HashSet<DeliverySpotValues>();
		values.add(value1);
		values.add(value2);
		values.add(value3);
		
		spot.setValues(values);
		
		Order order = new Order();
		order.setCustomer(c);
		order.setDeliverySpot(spot);
		order.setEstablishment(establishment);
		order.setOrderDate(new Date());
		saveOrderItems(order, items);
		order.save();
	}
	
	private void saveOrderItems(Order order, List<MenuItem> menuItems) {
		Set<OrderItem> items = new HashSet<OrderItem>();
		OrderItem item1 = new OrderItem();
		item1.setDetail("sem maionese");
		item1.setOrder(order);
		item1.setQtd(1);
		item1.setUnitPrice(menuItems.get(0).getPrice());
		item1.setItem(menuItems.get(0));
//		item1.save();

		OrderItem item2 = new OrderItem();
		item2.setDetail("sem catchup");
		item2.setOrder(order);
		item2.setQtd(2);
		item1.setUnitPrice(menuItems.get(1).getPrice());
		item2.setItem(menuItems.get(1));
//		item2.save();
		
		items.add(item1);
		items.add(item2);

		order.setItems(items);
	}
}
